package com.tevintao.marketingsystem.task;

import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ListView;

import com.tevintao.marketingsystem.log.LogManager;
import com.tevintao.marketingsystem.services.ViewOperation;
import com.tevintao.marketingsystem.xposted.WxTaskManger;


public class ChangeFriendCircleBackgroundTaskProcess extends SetTaskExtra {
    private static ChangeFriendCircleBackgroundTaskProcess ourInstance = new ChangeFriendCircleBackgroundTaskProcess();

    public static ChangeFriendCircleBackgroundTaskProcess getInstance() {
        return ourInstance;
    }

    private ChangeFriendCircleBackgroundTaskProcess() {
    }

    @SuppressWarnings("ResourceType")
    @Override
    public boolean processView(String contextName, View v) {
        Object iconObj = getExtra(WxUiViewConfig.CHANGE_ICON_PATH);
        if (null == iconObj) {
            taskError(v.getContext(), WxTaskManger.TASK_SATE_EXTRA_ERROR, "没有图片");
            return true;
        }
        if (WxUiViewConfig.MAIN_LAUNCH.equals(contextName)) {
            boolean inDiscover = WxUiViewConfig.inTab(v, WxUiViewConfig.TAB_DISCOVER);
            if (inDiscover) {
                View friendView = WxUiViewConfig.getViewByIdAndContent((ViewGroup) v, WxUiViewConfig.DISCOVER_LIST_ITEM_ID, "朋友圈");
                if (null != friendView) {
                    ViewOperation.clickView(friendView);
                }
            }
        } else if (WxUiViewConfig.CIRCLE_OF_FRIENDS.equals(contextName)) {
            View recover = v.findViewById(WxUiViewConfig.CIRCLE_OF_FRIEND_COVER);
            if (null != recover) {
                ViewOperation.clickView(recover);
            } else {
                View listView = v.findViewById(WxUiViewConfig.CIRCLE_OF_FRIENDS_LIST_VIEW);
                if (null != listView && listView instanceof ListView) {
                    ListView list = (ListView) listView;
                    ViewOperation.moveList2Header(list);
                }
            }
//            WxUiViewConfig.printViewGroup((ViewGroup) v);
        } else if (WxUiViewConfig.DIALOG_CLASS.equals(contextName)) {
            View changeCoverView = v.findViewById(WxUiViewConfig.CHANGE_COVER_ITEM);
            if (null != changeCoverView) {
                ViewOperation.clickView(changeCoverView);
            }
        } else if (WxUiViewConfig.VIEW_SETTING_SNS_BACKGROUND.equals(contextName)) {
            View selectFromLocal = WxUiViewConfig.getViewByIdAndContent((ViewGroup) v, WxUiViewConfig.SELECT_FROM_LOCAL_ID, "从手机相册选择");
            if (null != selectFromLocal) {
                ViewOperation.clickView(selectFromLocal);
            }
        } else if (WxUiViewConfig.VIEW_ALBUM_PREVIEW.equals(contextName)) {
            WxUiViewConfig.printViewGroup((ViewGroup) v);
            View gridView = v.findViewById(WxUiViewConfig.GRID_VIEW);
            if (null != gridView && gridView instanceof GridView) {
                ViewGroup gridGroup = (ViewGroup) gridView;
                if (gridGroup.getChildCount() > 1) {
                    View secondView = gridGroup.getChildAt(1);
                    if (null != secondView) {
                        ViewOperation.clickView(secondView);
                    }
                }
            }
        } else if (WxUiViewConfig.VIEW_CROP_IMAGE_NEW.equals(contextName)) {
            LogManager.getInstance().logInfo("点击使用按钮");
            View useButton = v.findViewById(WxUiViewConfig.USE_NEW_ICON_BUTTON);
            if (null != useButton) {
                ViewOperation.clickView(useButton);
                LogManager.getInstance().logInfo("点击使用按钮2");
                taskError(v.getContext(), WxTaskManger.TASK_SATE_SUCCESS, "切换图像成功");
            }
        }
        return false;
    }
}
